package hibernate.xml43.view;

import hibernate.xml43.pojo.Dept;
import hibernate.xml43.util.HibernateHandler;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class View3 {
	public static void main(String[] args) {
		addDept();
	}
	//事务模版
	public static void addDept() {
		Session session  = HibernateHandler.getSessionFactory().openSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
			Dept d = new Dept();
			d.setDname("Jack");
			d.setLoc("LA");
			session.save(d);
			transaction.commit();
		}catch(Exception e){
			//触发异常就回滚
			if(transaction!=null){
				transaction.rollback();
			}
			throw new RuntimeException(e.getMessage());
		}finally{
			//使用open获得的session必须关闭
			if(session!=null){
				session.close();
			}
		}
	}
}
